//
//  oldTopGroupPhotoVIew.m
//  laonianbangSupport
//
//  Created by 勇拓 李 on 18/3/7.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldTopGroupPhotoVIew.h"
#import "UIImageView+oldImageView.h"
#import "oldDateTools.h"

@interface oldTopGroupPhotoVIew()
@property (weak, nonatomic) IBOutlet UILabel *label_time;
@property (weak, nonatomic) IBOutlet UIImageView *img_groupPhoto;
@property (nonatomic, strong)NSTimer *timer;
@property (nonatomic, strong)NSDate *oldDate;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (nonatomic, assign)CGFloat height;
@property (nonatomic, assign)CGFloat width;
@end

//原始尺寸
static CGRect oldframe;

@implementation oldTopGroupPhotoVIew

- (void)awakeFromNib
{
    [super awakeFromNib];
    UIBezierPath* rounded = [UIBezierPath bezierPathWithRoundedRect:_topView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(10.0f, 10.0f)];
    CAShapeLayer* shape = [[CAShapeLayer alloc] init];
    [shape setPath:rounded.CGPath];
    
    UITapGestureRecognizer* tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImg:)];
    [_img_groupPhoto addGestureRecognizer:tapGes];
    _img_groupPhoto.userInteractionEnabled = YES;
    
    _topView.layer.mask = shape;
}

- (void)removeTopGroupPhotoView
{
    if (_timer) {
        if ([_timer isValid]) {
            [_timer invalidate];
        }
        _timer = nil;
    }
    
    [self removeFromSuperview];
}

- (void)initWithPhotoUrl:(NSString *)url
{
    [_img_groupPhoto showImage:url];
}

- (void)initSeverTimer:(NSString *)time
{
    if (time) {
        _timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(date_calculate) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
        [self date_calculate];
        _oldDate = [oldDateTools NSstringToNSdate:time];
    }
    else
    {
        [_topView removeFromSuperview];
    }
}

- (void)date_calculate
{
    _label_time.text = [NSString stringWithFormat:@"服务时间:%@", [oldDateTools getDateFromDateToNow:_oldDate]];
}

/**
 *  浏览大图
 *
 */
- (void)tapImg:(UIGestureRecognizer *)ges
{
    //当前imageview的图片
    UIImage *image = self.img_groupPhoto.image;
    //当前视图
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //背景
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    //当前imageview的原始尺寸->将像素currentImageview.bounds由currentImageview.bounds所在视图转换到目标视图window中，返回在目标视图window中的像素值
    oldframe = [self convertRect:self.bounds toView:window];
    oldframe = CGRectMake(oldframe.origin.x, oldframe.origin.y + 20, oldframe.size.width, oldframe.size.height);
    NSLog(@"1:%@", NSStringFromCGRect(oldframe));
    [backgroundView setBackgroundColor:Color_With_Rgb(107, 107, 99, 1)];
    //此时视图不会显示
    [backgroundView setAlpha:0];
    //将所展示的imageView重新绘制在Window中
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:oldframe];
    
    [imageView setImage:image];
    [imageView setTag:101];
    [backgroundView addSubview:imageView];
    //将原始视图添加到背景视图中
    [window addSubview:backgroundView];
    NSLog(@"4:%@", NSStringFromCGRect(imageView.frame));
    
    
    //添加点击事件同样是类方法 -> 作用是再次点击回到初始大小
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideImageView:)];
    [backgroundView addGestureRecognizer:tapGestureRecognizer];
    
    //动画放大所展示的ImageView
    
    [UIView animateWithDuration:0.5 animations:^{
        CGFloat y,width,height;
        y = ([UIScreen mainScreen].bounds.size.height - image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width) * 0.5;
        //宽度为屏幕宽度
        width = [UIScreen mainScreen].bounds.size.width;
        //高度 根据图片宽高比设置
        height = image.size.height * [UIScreen mainScreen].bounds.size.width / image.size.width;
        [imageView setFrame:CGRectMake(0, y, width, height)];
        //重要！ 将视图显示出来
        [backgroundView setAlpha:1];
    } completion:^(BOOL finished) {
        
    }];
    
}

/**
 *  恢复imageView原始尺寸
 *
 *  @param tap 点击事件
 */
- (void)hideImageView:(UITapGestureRecognizer *)tap{
    UIView *backgroundView = tap.view;
    //原始imageview
    UIImageView *imageView = [backgroundView viewWithTag:101];
    
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    //恢复
    [UIView animateWithDuration:0.5 animations:^{
        [imageView setFrame:oldframe];
        [backgroundView setAlpha:0];
    } completion:^(BOOL finished) {
        //完成后操作->将背景视图删掉
        [backgroundView removeFromSuperview];
    }];
}

@end
